package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.servie.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {

    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);
    //入口

    public void start() {//this


        loop:
        while (true) {
            //展示菜单
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            //录入数据
            String choice = sc.nextLine();
            //switch 选择
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudents();
                    break;
                case "5":
                    System.out.println("退出学生,回到主界面");
                    // break loop;
                    return;

                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }

    public void updateStudent() {
        ArrayList<Student> stus = studentService.findAllStudents();
        if (stus == null || stus.size() == 0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String sid = getStudentSid();
        Student student = getStudentInfo(sid);
        studentService.updateStudent(sid, student);
        System.out.println("修改成功");


    }

    public String getStudentSid() {
        //录入学号
        String sid = null;
        while (true) {
            System.out.println("请输入要的学号");
            sid = sc.next();
            //调用service  判断是否存在
            boolean exist = studentService.isExist(sid);
            if (exist) {
                //存在 调用service  删除
                break;
            } else {
                //不存在 重新录
                System.out.println("你输入的学号不存在");
            }
        }
        return sid;
    }

    public void deleteStudent() {
        ArrayList<Student> stus = studentService.findAllStudents();
        if (stus == null || stus.size() == 0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        //录入学号
        String sid = getStudentSid();
        studentService.deleteStudentBySid(sid);
        System.out.println("删除成功");
    }

    //查询所有学生
    public void findAllStudents() {
        ArrayList<Student> stus = studentService.findAllStudents();
        if (stus == null||stus.size()==0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        //遍历数组  展示数据
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            if (stu != null) {
                System.out.println(stu.getSid() + "\t" + stu.getName()
                        + "\t" + stu.getAge() + "岁\t" + stu.getBirthday());
            }
        }


    }

    public void addStudent() {


        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean isExist = studentService.isExist(sid);
            if (isExist) {
                System.out.println("已经存在,请重新输入");
            } else {
                break;
            }
        }

        Student student = getStudentInfo(sid);
        //调用service  把学生对象 传过去

        studentService.addStudent(student);
        System.out.println("添加成功");

    }

    public Student getStudentInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //封装对象
        return new Student(sid, name, age, birthday);
    }
}
